我們曾經認識過陣列(array),
今天來介紹他的兄弟List吧
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一個List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
            Console.WriteLine("出來吧12生肖!");
            //我們使用foreach迴圈來列出List的值
            foreach(string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }
結果:
出來吧12生肖!
鼠
牛
虎
兔
龍
蛇
馬
羊
猴
雞
狗
豬
他的初始值給法跟陣列十分相似,但她不需要給初始長度
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一個List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
            //使用Add()新增熊
            myList.Add("熊");
            Console.WriteLine("出來吧13生肖!");
            //我們使用foreach迴圈來列出List的值
            foreach(string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }
結果:
出來吧13生肖!
鼠
牛
虎
兔
龍
蛇
馬
羊
猴
雞
狗
豬
熊
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一個List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
            //宣告一個要新增的List
            List<string> newList = new List<string>() { "熊", "象", "魚" };
            //AddRange()將newList新增到myList中
            myList.AddRange(newList);
            //使用Count()來表示List筆數
            Console.WriteLine("出來吧" + myList.Count() + "生肖!");
            //我們使用foreach迴圈來列出List的值
            foreach (string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
  }
結果:
出來吧15生肖!
鼠
牛
虎
兔
龍
蛇
馬
羊
猴
雞
狗
豬
熊
象
魚
我們這邊多使用了myList.Count()來列出目前List的總筆數
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一個List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬", "熊", "象", "魚" };
            //使用Remove()移除魚
            myList.Remove("魚");
            //使用Count()來表示List筆數
            Console.WriteLine("出來吧" + myList.Count() + "生肖!");
            //我們使用foreach迴圈來列出List的值
            foreach (string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }
結果:
出來吧14生肖!
鼠
牛
虎
兔
龍
蛇
馬
羊
猴
雞
狗
豬
熊
象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一個List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬", "熊", "象", "魚" };
            //由於第一筆是從0算起,所以要從12開始刪3筆
            myList.RemoveRange(12,3);
            //使用Count()來表示List筆數
            Console.WriteLine("出來吧" + myList.Count() + "生肖!");
            //我們使用foreach迴圈來列出List的值
            foreach (string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }
結果:
出來吧12生肖!
鼠
牛
虎
兔
龍
蛇
馬
羊
猴
雞
狗
豬
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    //建立一個class
    class DataModel
    {
        //月份
        public int month;
        //動物
        public string animal;
    }
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一個List
            List<DataModel> dataList = new List<DataModel>();
            //宣告一個List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
            //宣告整數來算月份
            int Count = 1;
            //我們使用foreach迴圈來填寫dataList的值
            foreach (string item in myList)
            {
                //新增一筆新的資料所以我們要使用new
                dataList.Add(new DataModel
                {
                    month = Count,
                    animal = item
                });
                //進入下個月
                Count++;
            }
            //使用Count()來表示List筆數
            Console.WriteLine("出來吧" + dataList.Count() + "生肖!");
            //我們使用foreach迴圈來列出List的值
            foreach (DataModel item in dataList)
            {
                //這邊需要用item.物件名稱,才有辦法取到值
                Console.WriteLine(item.month + "月 , 屬 :" + item.animal);
            }
            Console.ReadKey();
        }
    }
  }
結果:
出來吧12生肖!
1月 , 屬 :鼠
2月 , 屬 :牛
3月 , 屬 :虎
4月 , 屬 :兔
5月 , 屬 :龍
6月 , 屬 :蛇
7月 , 屬 :馬
8月 , 屬 :羊
9月 , 屬 :猴
10月 , 屬 :雞
11月 , 屬 :狗
12月 , 屬 :豬